﻿using DianPingDataGrab.Provider;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DianPingDataGrab
{
    public partial class MainForm : Form
    {
        public DianPingProvider provider = new DianPingProvider();
        public MainForm()
        {
            InitializeComponent();

            DataTable dt = provider.DtResult;

                foreach (DataColumn item in dt.Columns)
                {
                    var column = new DataGridViewTextBoxColumn();
                    column.HeaderText = item.ColumnName;
                    dGridDianPing.Columns.Add(column);
                }
        }

        private void btnSeach_Click(object sender, EventArgs e)
        {
            btnSeach.Text = "抓取中...";
            btnSeach.Enabled = false;
            new Task(() =>
           {
               ClearLog();
               provider.SearchData(dGridDianPing, txtLog);
               btnSeach.BeginInvoke((MethodInvoker)delegate ()
               {
                   btnSeach.Text = "开始抓取";
                   btnSeach.Enabled = true;
               });
               return;
           }).Start();
        }
        private void ClearLog()
        {
            txtLog.BeginInvoke((MethodInvoker)delegate ()
            {
                txtLog.Text = "";
            });
        }
        private void SetLog(string msg)
        {
            this.txtLog.BeginInvoke((MethodInvoker)delegate ()
            {
                this.txtLog.Text = $"{msg}{Environment.NewLine}" + this.txtLog.Text;
            });
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnExp_Click(object sender, EventArgs e)
        {
            if (dGridDianPing.Rows.Count <= 0)
            {
                MessageBox.Show("请先搜索数据！");
                return;
            }

            ExcelHelper.ExportExcel("抓取结果", dGridDianPing);
        }
    }
}
